/*
 * =====================================================================================
 *
 *       Filename:  cl_socket.hxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  03/24/2011 02:39:45 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_SOCKET_HXX_
#define _CL_SOCKET_HXX_

#include <memory>
#include <string>
#include "cl_socket_addr.hxx"
#include "cl_common_defs.hxx"

namespace neo
{
class CLBuffer;
class CLSocket
{
public:
  CLSocket(const std::string& host, int port, CLSocketType type);
  CLSocket(int socketfd, CLSocketAddrUPtr addr);
  virtual ~CLSocket();

  ssize_t send(const void* data, size_t siz);
  ssize_t sendOutLeft(void);
  ssize_t receive(void);
  bool hasDataToSend(void) const;
  bool shutDown(CLConnCloseType type);
  bool setBlock(bool isBlock);
  bool setReuseable(bool isReuseable);
  CLBuffer& readerBuf(void) const;
  CLBuffer& writerBuf(void) const;
  bool isValid(void) const;
  int fd(void) const;
  int lastErrorCode(void);

private:
  CLSocket(const CLSocket&);
  CLSocket& operator=(const CLSocket&);

protected:
  CLSocketAddrUPtr m_sockAddr;
  std::unique_ptr<CLBuffer> m_readerBuf;
  std::unique_ptr<CLBuffer> m_writerBuf;
  std::string m_host;
  int m_port;
  int m_socketfd; 
};

} // neo

#endif //_CL_SOCKET_HXX_

